﻿
using FineUIMvc;
using SqlSugar;
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BoYuan.DBServices;
using BoYuan.FineUIMvcWeb.Controllers;
using BoYuan.Framework.Uitility;

namespace BoYuan.FineUIMvcWeb.Areas.Admin.Controllers
{
    public partial class waterMarkObjController : BaseController
    {
        #region ListPage
        // GET: Admin/waterMarkObj
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Grid1_PageIndexChanged(FormCollection fc)
        {
            var db = DB_Base.Instance;

            //此处代码不能设置断点debug。
            var queryable = db.Queryable<BoYuan.Entity.waterMarkObj>()
                //todo 筛选
                //.WhereIF(fc["txb_keyword"].Trim().Length > 0, p=> )
                .OrderBy(p => p.id)
                .Select<BoYuan.Entity.waterMarkObj>() ;

            var grid1 = UIHelper.Grid("Grid1");
            grid1.BindGrid(fc, "ddlPageSize", queryable);
            return UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Button_delete_Click(string ids)
        {
            var idList = Array.ConvertAll<string, int>(ids.Substring(1).Split(','), int.Parse);
            if (idList.Length == 0)
            {
                NotifyQuestion("请选择要删除的选中行！");
            }
            else
            {
                var db = DBServices.DB_Base.Instance;
                db.Deleteable<BoYuan.Entity.waterMarkObj>(p => idList.Contains(p.id)).ExecuteCommand();
                AlertSuccess("删除成功！", false, "onGridBind()");
            }
            return UIHelper.Result();
        }
        #endregion

        #region AddPage

        public ActionResult Add()
        {
            if (int.TryParse(Request["id"],out int id))//编辑页面初始化
            {
                BoYuan.Entity.waterMarkObj mo = DBServices.DB_Base.GetModel<BoYuan.Entity.waterMarkObj>(id);

                return View(mo);
            }
            return View();//添加页面
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Button_save_OnClick(FormCollection fc)
        {
            if (string.IsNullOrEmpty(fc["f_img"]))
            {
                AlertError("背景图片必须上传");
                return UIHelper.Result();
            }

            bool isEdit = int.TryParse(fc["f_id"], out int id);

            BoYuan.Entity.waterMarkObj mo = new BoYuan.Entity.waterMarkObj();
            mo.title= fc["f_title"]; 
            mo.sort= Convert.ToInt32(fc["f_sort"]); 
            mo.pic= fc["f_img"]; 


            if (!isEdit)//添加
            {
                mo.pic2 = UploadFileCommand.GetNewSaveFileUrl();
                if (DBServices.DB_Base.InsertModel(mo) > 0)
                {
                    AlertInfor("添加成功", true);
                }
                else
                {
                    NotifyError("添加失败");
                }
            }
            else//修改
            {
                int returnNum= DBServices.DB_Base.UpdateModels(mo, 
                           //注意更新字段是否对应，数量是否一致 
p => new {p.title,p.sort,p.pic},
                           //条件
                           p => p.id==id);
                if (returnNum>0)
                {
                    AlertInfor("修改成功", true);
                }
                else
                {
                    NotifyError("修改失败");
                }
            }

            return UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult filePhoto_FileSelected(HttpPostedFileBase f_pic, FormCollection fc)
        {
            using (UploadFileCommand ufc=new UploadFileCommand(f_pic))
            {
                if (!ufc.UpLoadIMG())
                {
                    AlertError("上传图片错误:"+ufc.Error);
                }

                UIHelper.Image("f_img").ImageUrl(ufc.FileName_url);
            }

            UIHelper.FileUpload("f_pic").Reset();//重置表单
            return UIHelper.Result();
        }

        #endregion

    }
}
